
 ############################################################################
 ## WOMEN'S REPRESENTATION AND CORRUPTION - REPLICATION FILES FOR APPENDIX ##
 ############################################################################
 
 
 # This script replicates appendix materials for the paper on WR and Corruption in Mexico paper
 
 # Sections 6-7, and 9-22 are available in the STATA script (Appendix part 2.do)
 
 # Clean
 rm(list=ls())

 # Set directory
 setwd("")


 # Load useful packages
 library(ggplot2)
 library(stargazer)
 library(readstata13)
 library(Matching) 
 library(ebal) 
 
 
 
 ## SECTION 4) HISTOGRAM OF AUDIT ASSIGNMENT
 
 election <- read.dta13("election_models.dta")
 
 # Keep election years and no usos y costumbres
 election <- subset(election, election$election_year==1 & election$usos==0)
 
 
 ## Quick plot of previous audits
 ggplot(election, aes(x=previous_audits)) + 
         geom_histogram(binwidth=0.5) + theme_light() +
         xlab("Count of previous audits") + ylab("") +
         scale_color_grey() +
         theme(plot.margin=unit(c(1, 1 , .8, .5),"cm"),
               axis.title = element_text(margin = margin(20, 20, 20, 20)),
               axis.title.x.bottom = element_text(margin = margin(15, 0, 1, 0)),
               axis.title.y.left = element_text(margin = margin(0, 15, 0, 1)))
 
 
 
 
 ## SECTION 5) BALANCE TABLES 
 
 ## Keep 2019 and no usos
 balance <- subset(election, election$year==2019 & election$usos==0)
 
 # Treated and control groups
 balance$treat <- NA
 balance$treat[balance$l_prev_audits_ever>0] <- 1
 balance$treat[balance$l_prev_audits_ever==0] <- 0
 
 table(balance$treat, exclude=NULL)
 
 # Log
 balance$fem_pop_log <- log(balance$female_pop)
 
 
 ## Import additional covariates
 munis <- read.dta13("inegi_municipality.dta")
 
 # Keep most recent
 munis_audit <- subset(munis, munis$year==2018)
 balance$year[balance$year==2019] <- 2018
 
 # Merge
 balance <- merge(balance, munis_audit, by=c("cve_inegi","year"), all.x=T)

 # Log + 1 of irregs
 balance$l_universe <- log(balance$universe_adj + 1)
 
 ## Table
 balance_formula = treat ~  lpop + margin + volatility + 
         ratio_men_women_ + average_schooling_ + HDI_ +
         water_index_ + sewage_index_ + electricity_index_ 
 
 # Balance test
 balance = MatchBalance(balance_formula, data=balance, nboots=10000) 
 
 variable_names = all.vars(balance_formula)[-1] 

 balance_test = baltest.collect(balance, var.names = variable_names, after = FALSE)
 
 # Table A2
 a2 <- (balance_test = balance_test[, c("mean.Tr", "mean.Co", "T pval", "KS pval")])
 stargazer(a2)
 
 
 ## SECTION 8) SUMMARY STATS

 # Election models (Table A5)
 
 vars <- election[, c("elected_mayor_female" , "l_audit_year" , 
                      "l_audit_2yr", "l_audit_3yr" , "l_prev_audits_ever" , 
                      "prev_audit_1yr",
                      "prev_audit_3yr",
                      "prev_auditreleased_1yr" , 
                      "prev_auditreleased_3yr", 
                      "HDI" , "lpop")]

 stargazer(vars, summary.stat = c("n", "mean", "sd", "min", "max"))



 # Models for spending irregularities (Table A6)
 
 audit <- read.dta13("spending_irregularities.dta")
 
 
 vars2 <- audit[, c("irreg_adj", "gender", "ln1_irreg_adj", "l_prevaudits", "universe_adj", 
                    "HDI", "lpop")]
 
 stargazer(vars2, summary.stat = c("n", "mean", "sd", "min", "max"))
 
 
 

